package com.yyb.service.impl;

import com.yyb.constants.CoreConstant;
import com.yyb.domain.SysDept;
import com.yyb.dto.SysDeptAddDTO;
import com.yyb.dto.SysDeptUpdateDTO;
import com.yyb.exception.BusinessException;
import com.yyb.mapper.SysDeptMapper;
import com.yyb.service.SysDeptService;
import com.yyb.vo.SysDeptVO;
import com.yyb.vo.SysMenuVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * 
 * 
 */
@Service
public class SysDeptServiceImpl extends BaseServiceImpl<SysDept> implements SysDeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;
    /**
     * 查询所有部门数据返回树型结构的JSON
     *
     * @return
     */
    @Override
    public List<SysDeptVO> listDept() {
        List<SysDept> deptList=this.sysDeptMapper.queryAllDept();
        //把List<SysDept> 转成List<SysDeptVO>
        List<SysDeptVO> sysDeptVOS = deptList.stream().map(dept -> {
            SysDeptVO sysDeptVO = new SysDeptVO();
            BeanUtils.copyProperties(dept,sysDeptVO);
            return sysDeptVO;
        }).collect(Collectors.toList());
        return convertDeptTree(sysDeptVOS, CoreConstant.DEFAULT_PARENT_ID);
    }

    /**
     * 递归
     * 把没有层级关系的 List<SysDeptVO> 转成有层级关系的
     *
     * @param sysDeptVOS
     * @param parentId
     * @return
     */
    private List<SysDeptVO> convertDeptTree(List<SysDeptVO> sysDeptVOS, Long parentId) {
        List<SysDeptVO> roots = sysDeptVOS.stream()
                .filter(sysDeptVO -> sysDeptVO.getParentId().equals(parentId))
                .collect(Collectors.toList());
        roots.forEach(r -> r.setChildren(convertDeptTree(sysDeptVOS, r.getDeptId())));
        return roots;
    }

    /**
     * 根据ID查询部门
     *
     * @param deptId
     * @return
     */
    @Override
    public SysDept selectByPrimaryKey(Long deptId) {
        return this.sysDeptMapper.selectByPrimaryKey(deptId);
    }

    /**
     * 添加部门
     *
     * @param sysDeptAddDTO
     * @return
     */
    @Override
    public int insert(SysDeptAddDTO sysDeptAddDTO) {
        SysDept sysDept=new SysDept();
        BeanUtils.copyProperties(sysDeptAddDTO,sysDept);
        //设置其它属性
        Date date = new Date();
        sysDept.setCreateTime(date);
        sysDept.setCreateBy(getUsername());
        sysDept.setUpdateTime(date);
        return this.sysDeptMapper.insertSelective(sysDept);
    }

    /**
     * 修改部门
     *
     * @param sysDeptUpdateDTO
     * @return
     */
    @Override
    public int updateByPrimaryKey(SysDeptUpdateDTO sysDeptUpdateDTO) {
        SysDept sysDept=new SysDept();
        BeanUtils.copyProperties(sysDeptUpdateDTO,sysDept);
        //设置其它属性
        Date date = new Date();
        sysDept.setUpdateTime(date);
        sysDept.setUpdateBy(getUsername());
        return this.sysDeptMapper.updateByPrimaryKeySelective(sysDept);
    }

    /**
     * 删除部门
     *
     * @param deptId
     * @return
     */
    @Override
    @Transactional
    public int deleteByPrimaryKey(Long deptId) {
        //根据部门ID查询这个ID下的子部门的数量
        Long count=this.sysDeptMapper.queryChildrenCountByDeptId(deptId);
        if(count>0){
            throw new BusinessException("当前部门下有子节点，不能直接删除，请先删除子部门");
        }
        return this.sysDeptMapper.deleteByPrimaryKey(deptId);
    }
}
